package com.zlx.sharelive.activity;

import android.content.Intent;
import android.text.Html;
import android.view.View;
import android.widget.RelativeLayout;
import android.widget.TextView;

import androidx.recyclerview.widget.GridLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.zlx.module_base.base_ac.BaseAc;
import com.zlx.module_base.base_api.res_data.AgreementBean;
import com.zlx.module_base.base_api.res_data.ChargeBean;
import com.zlx.module_base.base_api.util.ApiUtil;
import com.zlx.module_base.base_util.SPUtil;
import com.zlx.module_network.api1.livedata.BaseObserver;
import com.zlx.module_network.api1.livedata.BaseObserverCallBack;
import com.zlx.module_network.bean.ApiResponse;
import com.zlx.sharelive.R;
import com.zlx.sharelive.adapter.CommonRecyclerViewAdapter;
import com.zlx.sharelive.adapter.CommonRecyclerViewHolder;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.OnClick;

/**
 * 充值页面
 */
public class RechargeAc extends BaseAc {

    @BindView(R.id.rv_data)
    RecyclerView rv_data;
    @BindView(R.id.tv_guize)
    TextView tv_guize;

    private List<ChargeBean.InfoDTO> mList = new ArrayList<>();
    private CommonRecyclerViewAdapter<ChargeBean.InfoDTO> mAdapter;

    @Override
    protected int getLayoutId() {
        return R.layout.ac_recharge;
    }

    @Override
    public void initViews() {
        super.initViews();

        initRecyclerView();
        getChargeList();
        getAgreement("6");
    }

    /**
     * 获取当前充值套餐列表
     */
    private void getChargeList() {
        ApiUtil.getMoneyApi().getChargeList(SPUtil.getToken(RechargeAc.this))
                .observe(this, new BaseObserver<>(new BaseObserverCallBack<ApiResponse<ChargeBean>>() {
                    @Override
                    public void onSuccess(ApiResponse<ChargeBean> data) {
                        mList.clear();
                        mList.addAll(data.getData().getInfo());
                        for (int i = 0; i < mList.size(); i++) {
                            mList.get(i).setCheck(false);
                        }
                        mAdapter.notifyDataSetChanged();
                    }

                    @Override
                    public void onFinish() {
                        super.onFinish();

                    }
                }));
    }

    /**
     * 初始化数据
     */
    private void initRecyclerView() {
        rv_data.setLayoutManager(new GridLayoutManager(this, 3));

        mAdapter = new CommonRecyclerViewAdapter<ChargeBean.InfoDTO>(this, mList) {
            @Override
            public void convert(CommonRecyclerViewHolder holder, ChargeBean.InfoDTO entity, int position) {
                RelativeLayout rl_bg = holder.getView(R.id.rl_bg);
                TextView tv_text1 = holder.getView(R.id.tv_text1);
                TextView tv_text2 = holder.getView(R.id.tv_text2);

                if (entity.isCheck() == true) {
                    rl_bg.setBackgroundResource(R.mipmap.recharge_true_img);
                    tv_text1.setTextColor(getResources().getColor(R.color.white));
                    tv_text2.setTextColor(getResources().getColor(R.color.white));
                } else {
                    rl_bg.setBackgroundResource(R.mipmap.recharge_false_img);
                    tv_text1.setTextColor(getResources().getColor(R.color.red));
                    tv_text2.setTextColor(getResources().getColor(R.color.black));
                }

                tv_text1.setText(entity.getMoney() + "元");
                tv_text2.setText("送" + entity.getMoneyPresented() + "元");
            }

            @Override
            public int getLayoutViewId(int viewType) {
                return R.layout.item_recharge;
            }
        };

        rv_data.setAdapter(mAdapter);
    }

    private void getAgreement(String type) {
        ApiUtil.getHomeApi().getAgreement(type).observe(this, new BaseObserver<>(new BaseObserverCallBack<ApiResponse<AgreementBean>>() {
            @Override
            public void onSuccess(ApiResponse<AgreementBean> data) {
                if (null != data) {
                    if (null != data.getData()) {
                        if (data.getData().getCode() == 0) {
                            if (null != data.getData().getInfo()) {
                                if (data.getData().getInfo().size() > 0) {
                                    tv_guize.setText(Html.fromHtml(data.getData().getInfo().get(0).getPost_content(), null, null));
                                }
                            }
                        }
                    }
                }
            }

            @Override
            public void onFinish() {
                super.onFinish();

            }
        }));
    }


    @OnClick({R.id.iv_back, R.id.tv_details})
    public void setOnClick(View view) {
        switch (view.getId()) {
            case R.id.iv_back:
                finish();
                break;
            //充值明细
            case R.id.tv_details:
                startActivity(new Intent(this, RechargeDetailsAc.class));
                break;
        }
    }


}
